package com.itboye.api.domain;

import com.github.pagehelper.Page;
import com.itboye.api.model.ByResponseData;
import com.itboye.mybatis.helper.ByPageInfoHelper;
import com.itboye.mybatis.model.PostModel;
import com.itboye.services.cms.interfaces.IPostService;
import com.itboye.system.exception.ByBaseException;
import com.itboye.system.helper.ByConsoleHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * PostDomain.java
 * Created on  2016/7/25 16:09
 * 版本       修改时间          作者      修改内容
 * V1.0.1    2016/7/25      hebidu    初始版本
 * <p>
 * Copyright (c) 2013 杭州博也网络科技有限公司 版权所有
 * HANGZHOU BOYE TECHNOLOGY CO.,LTD. All Rights Reserved.
 */
@Component("PostDomain")
public class PostDomain extends BaseDomain {

    private static final Logger logger = LogManager.getLogger(PostDomain.class.toString());

    @Resource
    private IPostService postService;

    public PostDomain(){
        super();
        ByConsoleHelper.debug("PostDomain init",logger);
    }


    /**
     * 查询分页
     * @return
     */
    public ByResponseData query() throws ByBaseException, UnsupportedEncodingException {
        ByConsoleHelper.debug("当前线程"+Thread.currentThread().getId(),logger);
        System.out.println("当前参数"+ getParams().toString());

        this.checkApiVersion("102");

        HashMap<String,String> params = new HashMap<String, String>();

        String category = this.getStringParam("cate");
        Integer pageNum = this.getIntegerParam("page_num");
        Integer pageSize = this.getIntegerParam("page_size");

        assertNotNull(category,"缺少分类ID");

        if(pageSize == null || pageNum == null){
            throw  new ByBaseException("缺少page_num或page_size参数");
        }


        params.put("post_category",category);

        String fields = "post_modified,comment_status,post_type,main_img,post_cate_name,post_category,post_date,id,jump_url,start_time,end_time,post_author,post_author_name,nickname";
        String orders = "post_modified desc";

        ByPageInfoHelper<PostModel> postModelPage = postService.queryByPaging(params,pageNum,pageSize,fields,orders);

        return success(postModelPage);

    }

}
